package com.symone.webcms.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.symone.webcms.mapper.DataContentMapper;
import com.symone.webcms.mapper.ModuleFieldMapper;
import com.symone.webcms.mapper.ModuleMapper;
import com.symone.webcms.model.DataContent;
import com.symone.webcms.model.Module;
import com.symone.webcms.model.ModuleField;
import com.symone.webcms.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *　　
 *   @description : Module 服务实现类
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-01-09
 */
@Service
public class ModuleServiceImpl extends ServiceImpl<ModuleMapper, Module> implements ModuleService {

    @Autowired
    private ModuleFieldMapper fieldMapper;
    @Autowired
    private DataContentMapper contentMapper;


    @Override
    public Page<Module> findPage(Page<Module> page, Wrapper<Module> module) {
        List<Module> result = baseMapper.selectPage(page,module);
        page.setRecords(result);
        return page;
    }

    @Override
    public List<Module> findAll() {
        Wrapper<Module> ew = new EntityWrapper<Module>();
        ew.where("deleted=0");
        return baseMapper.selectList(ew);
    }

    @Override
    public int delete(long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public boolean deleteFlagById(long id) {
        return baseMapper.deleteFlagById(id);
    }

    @Override
    public int clear() {
        return baseMapper.delete(new EntityWrapper<Module>().eq("deleted" ,1));
    }

    @Override
    @Transactional
    public boolean deleteRelation(Long id) {
        Module module = baseMapper.selectById(id);
        fieldMapper.delete(new EntityWrapper<ModuleField>().eq("module",module.getCode()));
        baseMapper.deleteById(id);
        return false;
    }

    @Override
    @Transactional
    public int getRecordCount(Long id) {
        Module module = baseMapper.selectById(id);
        return contentMapper.selectCount( new EntityWrapper<DataContent>().eq("module",module.getCode()) );
    }


}
